## ----setup, include=FALSE-----------------------
knitr::opts_chunk$set(message = FALSE, error = F, echo = F)

# library -----------------------------------------------------------------
library(tidyverse)
library(janitor)
library(formattable)
library(kableExtra)
library(scales)
library(hrbrthemes)
library(htmltools)
library(reactable)
library(ggchicklet)
library(ggtext)
library(glue)
library(tidyquant)
library(qdapRegex)
library(ggrepel)
library(corrplot)
library(sjPlot)
library(GGally)
library(quanteda)
library(quanteda.textstats)
library(stringdist)

source("style.R")

# p value
# mat : is a matrix of data
# ... : further arguments to pass to the native R cor.test function
cor.mtest <- function(mat, ...) {
    mat <- as.matrix(mat)
    n <- ncol(mat)
    p.mat<- matrix(NA, n, n)
    diag(p.mat) <- 0
    for (i in 1:(n - 1)) {
        for (j in (i + 1):n) {
            tmp <- cor.test(mat[, i], mat[, j], ...)
            p.mat[i, j] <- p.mat[j, i] <- tmp$p.value
        }
    }
  colnames(p.mat) <- rownames(p.mat) <- colnames(mat)
  p.mat
}


gpairs_lower <- function(g){
  g$plots <- g$plots[-(1:g$nrow)]
  g$yAxisLabels <- g$yAxisLabels[-1]
  g$nrow <- g$nrow -1
  
  g$plots <- g$plots[-(seq(g$ncol, length(g$plots), by = g$ncol))]
  g$xAxisLabels <- g$xAxisLabels[-g$ncol]
  g$ncol <- g$ncol - 1
  
  g
}

options(digits=2)
options(scipen = 999)


# Data --------------------------------------------------------------------
load("mudancas_climaticas_data_br_full.Rdata")



## ----geral_kpi, align = "center", message = F, error=F, echo = F, warning = F----

ag <-  agregado %>%
  summarise(Posts = n(), 
             mean_interactions = mean(total_interactions, na.rm = T),
             total_interactions = sum(total_interactions, na.rm = T)) %>% 
  gather(key = "Categoria", value = "Valores")



kpi <- reactable(
  ag,
  highlight = T,
  fullWidth = F,
  defaultColDef = colDef(align = "center", minWidth = 200),
  columns = list(
    Valores = colDef(name = "Valores", 
                          cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  style = list(fontFamily = "Roboto Condensed")
)


div(kpi, align = "center")


## ----geral_fb, align = "center", message = F, error=F, echo = F, warning = F----

ag_fb <- fb %>%
  summarise(
    Posts = n_distinct(url, na.rm = T),
    Likes = as.integer(sum(likes, na.rm = T)),
    Likes_mean = as.integer(mean(likes, na.rm = T)),
    Views = as.integer(sum(total_views, na.rm = T)),
    Comments = as.integer(sum(comments, na.rm = T)),
    Comments_mean = as.integer(mean(comments, na.rm = T)),
    Shares = as.integer(sum(shares, na.rm = T))
  ) %>%
  pivot_longer(cols = Posts:Shares, names_to = "Métrica") %>%
  mutate(Métrica = prettyNum(Métrica, big.mark = ".", decimal.mark = ",")) %>%
  rename(Valor = value)


rct1 <- reactable(
  ag_fb,
  highlight = T,
  fullWidth = F,
  columns = list(
    Valor = colDef(name = "Valores", 
                     cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  defaultColDef = colDef(align = "center", minWidth = 200),
  style = list(fontFamily = "Roboto Condensed")
)


div(rct1, align = "center")


## ----geral_insta, align = "center", message = F, error=F, echo = F, warning = F----


ag_insta <- insta %>%
  summarise(
    Posts = n_distinct(url, na.rm = T),
    Likes = as.integer(sum(likes, na.rm = T)),
    Likes_mean = as.integer(mean(likes, na.rm = T)),
    Views = as.integer(sum(total_views, na.rm = T)),
    Comments = as.integer(sum(comments, na.rm = T)),
    Comments_mean = as.integer(mean(comments, na.rm = T))) %>%
  pivot_longer(cols = Posts:Comments_mean, names_to = "Métrica") %>%
  mutate(Métrica = prettyNum(Métrica, big.mark = ".", decimal.mark = ",")) %>%
  rename(Valor = value)


rct2 <- reactable(
  ag_insta,
  highlight = T,
  fullWidth = F,
  columns = list(
    Valor = colDef(name = "Valores", 
                   cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  defaultColDef = colDef(align = "center", minWidth = 200),
  style = list(fontFamily = "Roboto Condensed")
)


div(rct2, align = "center")


## ----geral_itw, align = "center", message = F, error=F, echo = F, warning = F----


ag_tw <- twitter %>%
  summarise(
    Posts = n_distinct(post_id, na.rm = T),
   Likes = as.integer(sum(public_metrics.like_count, na.rm = T)),
    Likes_mean = as.integer(mean(public_metrics.like_count, na.rm = T)),
   Comments = as.integer(sum(public_metrics.reply_count, na.rm = T)),
    Comments_mean = as.integer(mean(public_metrics.reply_count, na.rm = T)),
    RTs_mean = as.integer(mean(public_metrics.retweet_count, na.rm = T)),
    RTs = as.integer(sum(public_metrics.retweet_count, na.rm = T))
   ) %>%
  pivot_longer(cols = Posts:RTs, names_to = "Métrica") %>%
  mutate(Métrica = prettyNum(Métrica, big.mark = ".", decimal.mark = ",")) %>%
  rename(Valor = value)


rct2 <- reactable(
  ag_tw,
  highlight = T,
  fullWidth = F,
  columns = list(
    Valor = colDef(name = "Valores", 
                   cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  defaultColDef = colDef(align = "center", minWidth = 200),
  style = list(fontFamily = "Roboto Condensed")
)


div(rct2, align = "center")


## ----ag_mensal, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=8----


t <- agregado  %>% 
  count(mes_dia) 


t %>% 
  ggplot(aes(x = mes_dia, y = n)) + 
  geom_line(aes(group = 1), color= cor, size = 3)+
  geom_point(color=cor, fill = "white", size = 3.5, shape = 21)+
  geom_text_repel(data = t %>%  top_n(3, n), aes(label= prettyNum(n, 
                                 big.mark = ".")), 
            hjust=-.6, vjust=.4, size = 7, 
            family = "Roboto Condensed",
            color = cor)  + # keep expand
  labs(x="", y="Posts\n",
        caption="<br>**Source**: Twitter Instagram e Facebook<br>") + 
 theme(legend.position = "none") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
                       date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(
    plot.title.position = "plot",
    plot.caption = element_textbox_simple(hjust = 0, halign = T),
    plot.subtitle = element_textbox_simple(
      lineheight = 1,
      padding = margin(5.5, 5.5, 5.5, 5.5),
      margin = margin(0, 0, 5.5, 0)
    )) +
  scale_y_continuous(labels = addUnits)





## ----ag_mensal_imprensa, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=8----


press_br %>% 
  ggplot(aes(x = mes_dia, y = n, group = press, color= press)) + 
  geom_line(size = 3, alpha  = .7)+
  geom_point(fill = "white", size = 3.5, shape = 21)+
  geom_text_repel(data = press_br %>% group_by(press) %>%  top_n(1, n), aes(color = press,
    label= prettyNum(n, 
                                 big.mark = ".")), 
            hjust=-.6, vjust=.4, size = 7, fontface = "bold",
            family = "Roboto Condensed")  + # keep expand
  labs(x="", y="Posts\n",
        caption="<br>**Source**: Globo e Folha<br>") + 
 theme(legend.position = "none") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(
    plot.title.position = "plot",
    plot.caption = element_textbox_simple(hjust = 0, halign = T),
    plot.subtitle = element_textbox_simple(
      lineheight = 1,
      padding = margin(5.5, 5.5, 5.5, 5.5),
      margin = margin(0, 0, 5.5, 0)
    )) +
  scale_y_continuous(labels = addUnits)





## ----ag_day, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=8----


t <- agregado %>% 
  count(dia) %>% 
  na.omit() 

t %>% 
  ggplot(., aes(x = dia, y = n)) +
  geom_line(color= "black", alpha = .2) + 
  geom_ma(color= cor,ma_fun = EMA, n = 7, linetype = 1, size = 1.5) +
  geom_text_repel(data = t %>%  top_n(1, n), 
                  aes(label= paste0(dia, "\n",
                                    prettyNum(n, big.mark = "."))
                                                 ), 
            hjust=-.6, vjust=.4, size = 7, 
            family = "Cambria",
            color = cor) +
  labs(x="", y="Pposts\n",
         caption="<br>**Source**: Twitter, Instagram e Facebook<br>") + 
  theme(legend.position = "none") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(
    plot.title.position = "plot",
    plot.caption = element_textbox_simple(hjust = 0, halign = T),
    plot.subtitle = element_textbox_simple(
      lineheight = 1,
      padding = margin(5.5, 5.5, 5.5, 5.5),
      margin = margin(0, 0, 5.5, 0)
    )) +
  scale_y_continuous(labels = addUnits)




## ----ag_comp, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----
t <- agregado %>% 
  count(dia, platform) %>% 
  na.omit() 

t %>% 
    ggplot(., aes(x = dia, y = n, group = platform)) +
  geom_line(aes(color= platform), alpha = .2) + 
  geom_ma(aes(color= platform), ma_fun = EMA, n = 7, linetype = 1, size = 1.5)  +
  labs(x="", y="Posts\n",
          caption="<br>**Source**: Twitter Instagram e Facebook<br>") + 
  facet_wrap(~ platform, nrow = 3, scales = "free") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(plot.caption = element_textbox_simple(hjust = 0, halign = T),
   ) +
  scale_y_continuous(labels = addUnits)+ 
  theme(legend.position = "bottom") +
  theme(strip.text.x = element_text(size = 11,  color = "grey20"))




## ----ag_comp_month, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----
t <- agregado %>% 
  count(mes_dia, platform) %>% 
  na.omit() %>%  bind_rows(press_agregado)

t %>% 
    ggplot(aes(x = mes_dia, y = n, group = platform, color= platform)) +
  geom_line(size = 2) + 
  geom_point(fill = "white", size = 2, shape = 21)+ 
  labs(x="", y="Posts\n",
          caption="<br>**Source**: Twitter Instagram e Facebook<br>") + 
  facet_wrap(~ platform, nrow = 4, scales = "free") +
  scale_x_date(limits = as.Date(c('2014/1/1', '2022/12/1')),
               date_breaks   = "2 months", 
               labels=date_format("%m\n%y"), 
               expand = c(0,0))+ tema +
  theme(plot.caption = element_textbox_simple(hjust = 0, halign = T),
   ) +
  scale_y_continuous(labels = addUnits)+ 
  theme(legend.position = "none") +
  theme(strip.text.x = element_text(size = 11,  color = "grey20"))




## ----correl_data, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----
t <- agregado %>% 
  count(mes_dia, platform) %>% 
  na.omit() %>%  bind_rows(press_agregado) %>%
  pivot_wider(names_from = platform, values_from = n) %>% 
  na.omit()

rownames(t) <- t$mes_dia
t$mes_dia <- NULL



## ----cor, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----
df_cor <- cor(t)


kpi <- reactable(
  df_cor,
  highlight = T,
  fullWidth = F,
  defaultColDef = colDef(align = "center", minWidth = 100, 
                         format = colFormat(digits = 4)),
  style = list(fontFamily = "Cambria" 
               )
)


div(kpi, align = "center")




## ----correl_p, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----

# matrix of the p-value of the correlation
p.mat <- cor.mtest(t)


kpi <- reactable(
  p.mat,
  highlight = T,
  fullWidth = F,
  defaultColDef = colDef(align = "center", 
                         format = colFormat(digits = 4),
                         minWidth = 100),
  columns = list(
    Valores = colDef(name = "Valores", 
                          cell = function(value){value <- format(value, big.mark = ".")}
    )
  ),
  style = list(fontFamily = "Cambria")
)


div(kpi, align = "center")




## ----correl_plot, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----

t_sep <- agregado %>% 
  count(mes_dia) %>% 
  add_column(platform = "Social_media") |> 
  na.omit() %>%  bind_rows(press_agregado) %>%
  pivot_wider(names_from = platform, values_from = n) %>% 
  na.omit()

rownames(t_sep) <- t_sep$mes_dia
t_sep$mes_dia <- NULL


tab_corr(t_sep, show.p = T,  p.numeric = T)



## ----correl_plot_sep, align = "center", message = F, error=F, echo = F, warning = F, fig.width=12, fig.height=14----

#col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
#corrplot(df_cor, method="color", col=col(200),  
#         type="upper", order="hclust", number.cex = 2,
#         cl.cex = 2,
#         addCoef.col = "black", # Add coefficient of correlation
#         tl.col="black", tl.cex =  2, 
#         #tl.srt=45, #Text label color and rotation
         # Combine with significance
#         p.mat = p.mat, 
#         sig.level = -1, 
#         insig = "p-value", 
         # hide correlation coefficient on the principal diagonal
#         diag=FALSE  
#         ) 


tab_corr(t, show.p = T,  p.numeric = T)



## ----scatterplot, align = "center", message = F, error=F, echo = F, warning = F, fig.width=10, fig.height=8----

g <- ggpairs(t, 
             lower  = list(continuous=wrap("smooth", colour="skyblue", fill = "skyblue")) ,
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag") 
)+ tema  +
  theme(strip.text.x = element_text(size = 11,  color = "grey20")) +
  theme(strip.text.y = element_text(size = 11, color = "grey20"))  +
  scale_y_continuous(labels = addUnits) +
  scale_x_continuous(labels = addUnits)

gpairs_lower(g)






## ----scatterplot_sep, align = "center", message = F, error=F, echo = F, warning = F, fig.width=10, fig.height=8----

g <- ggpairs(t_sep, 
             lower  = list(continuous=wrap("smooth", colour="skyblue", fill = "skyblue")) ,
             upper  = list(continuous = "blank"),
             diag  = list(continuous = "blankDiag") 
)+ tema  +
  theme(strip.text.x = element_text(size = 11,  color = "grey20")) +
  theme(strip.text.y = element_text(size = 11, color = "grey20"))  +
  scale_y_continuous(labels = addUnits) +
  scale_x_continuous(labels = addUnits)

gpairs_lower(g)





